package com.example.demo.service.impl;

import com.example.demo.domain.Feedback;
import com.example.demo.enums.StatusEnum;
import com.example.demo.enums.YesOrNo;
import com.example.demo.repository.FeedbackRepository;
import com.example.demo.service.BaseService;
import com.example.demo.service.FeedbackService;
import com.example.demo.web.rest.evt.IdEvt;
import com.example.demo.web.rest.evt.SaveFeedbackEvt;
import com.example.demo.web.rest.evt.SaveReplyFeedbackEvt;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @Author zhengmf
 * @Date 2019/1/2  15:49
 */
@Service
@Transactional
public class FeedbackServiceImpl extends BaseService implements FeedbackService {
    @Autowired
    private FeedbackRepository feedbackRepository;

    @Override
    public Long saveFeedback(SaveFeedbackEvt evt) {
        Feedback feedback = new Feedback();
        BeanUtils.copyProperties(evt,feedback);
        feedback.setCreateName(getCurrentUserAlias());
        feedback.setCreateTime(new Date());
        feedback.setIsRead(YesOrNo.N.name());
        feedback.setStatus(StatusEnum.E.name());
        Feedback newFeedback = feedbackRepository.save(feedback);
        return newFeedback.getId();
    }

    @Override
    public Long saveReplyFeedback(SaveReplyFeedbackEvt evt) {
        Feedback feedback = new Feedback();
        BeanUtils.copyProperties(evt,feedback);
        feedback.setReplyName(getCurrentUserAlias());
        feedback.setReplyTime(new Date());
        feedback.setStatus(StatusEnum.E.name());
        Feedback newFeedback = feedbackRepository.save(feedback);
        return newFeedback.getId();
    }

    @Override
    public void updateStatus(IdEvt evt) {
        feedbackRepository.updateStatus(StatusEnum.D.name(),evt.getId());
    }

}
